package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.conn.JsonRechargeAsyGet;

import java.util.List;

/**
 * Created by Administrator on 2015/11/16.
 */
public abstract class RechargeAdapter extends ArrayAdapter<JsonRechargeAsyGet.Info>{

    private LayoutInflater layoutInflater;

    private int cuurentPosition = 0;

    public RechargeAdapter(Context context, List<JsonRechargeAsyGet.Info> objects) {

        super(context, 0, objects);

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final JsonRechargeAsyGet.Info info = getItem(position);

        ViewHolder viewHolder;

        if(convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_recharge, null)));

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();

        }

        if(cuurentPosition == position){

            viewHolder.button.setBackgroundResource(R.drawable.shape_radius_solid_blue);

            viewHolder.unit.setTextColor(Color.parseColor("#ffffff"));

            viewHolder.money.setTextColor(Color.parseColor("#ffffff"));

            onItemSelect(info);

        }else{

            viewHolder.button.setBackgroundResource(R.drawable.shape_radius_solid_grey_grey);

            viewHolder.unit.setTextColor(Color.parseColor("#666666"));

            viewHolder.money.setTextColor(Color.parseColor("#666666"));

        }

        viewHolder.money.setText(info.acount);

        viewHolder.button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                cuurentPosition = position;

                onItemSelect(info);

                notifyDataSetChanged();

            }

        });

        return convertView;

    }

    private class ViewHolder{

        @BoundView(R.id.item_recharge_button) private View button;

        @BoundView(R.id.item_recharge_unit) private TextView unit;

        @BoundView(R.id.item_recharge_money) private TextView money;

    }

    protected abstract void onItemSelect(JsonRechargeAsyGet.Info info);

}
